package com.zhz.web;

import com.zhz.servlet.ZhzServlet;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.ServletRegistration;

/**
 * @author zhouhengzhe
 * @description:
 * @date 2021/11/30 下午8:14
 * @since v1
 */
public class ZhzExternalSpringBootApplication {
	private static int port = 8099;
	private static String contextPath = "/zhz";

	public static void run() {
		Tomcat tomcat = new Tomcat();
		String baseDir = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		tomcat.setPort(port);

		try {
			Context context = tomcat.addContext(contextPath, baseDir);

			//ServletContextInitializer
			context.addServletContainerInitializer((c, servletContext) -> {

				ServletRegistration.Dynamic tulingServlet = servletContext.addServlet("ZhzServlet", new ZhzServlet());
				tulingServlet.addMapping("/zhzHello");
			}, null);

			// 启动tomcat
			tomcat.start();

		} catch (LifecycleException e) {
			e.printStackTrace();
		}
		// 挂起tomcat
		tomcat.getServer().await();

	}
}
